package com.daka.controller;

import com.daka.entity.*;
import com.daka.enumerate.StatusCode;
import com.daka.service.*;
import com.daka.service.qnzyz.BHOrgService;
import com.daka.tools.StrUtils;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.GroupResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/8/3 0003
 * Time: 11:05
 */
@Controller
@RequestMapping("/group")
@Slf4j
public class GroupController {

    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private GroupServiceDomainService groupServiceDomainService;
    @Autowired
    BHOrgService bhOrgService;


    /**
     *  个人组织
     * @param volunteerId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryList")
    public DataResponseVo<List> queryList(@RequestParam(value = "volunteerId")String volunteerId) {
        try {
            System.out.println(volunteerId);
            List<Group> list = groupService.queryListByVolunteer(Long.valueOf(volunteerId));
            List<GroupResponseVo> list1 = new ArrayList<GroupResponseVo>();
            if(list.size()>0){
                for(Group aa:list){
                    list1.add(GroupResponseVo.buildVO(aa));
                }
            }
            return new DataResponseVo<List>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),list1);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<List>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    /**
     * 组织详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDetail")
    public DataResponseVo<Object> queryDetail(@RequestParam(value = "id")String id) {
        try {
            System.out.println(id);
            Group  group = groupService.selectByPrimaryKey(Long.valueOf(id));
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),group);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping(value = "/insert")
    public DataResponseVo<Object> insert(Group group,@RequestParam(value = "userId")String userId) {
        try {
            if (StrUtils.isEmpty(group.getName())) {
                return new DataResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            Group param =new Group();
            param.setName(group.getName());
            List<Group> list = groupService.selectByStatement(param);
            if(list!=null){
                return new DataResponseVo<Object>(StatusCode.FILE_EXIT.getCode(),StatusCode.FILE_EXIT.getMessage());
            }
            int a = groupService.insert(group, Long.valueOf(userId));

            if (a < 1) {
                return new DataResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
            }
            return  new DataResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }



    @ResponseBody
    @RequestMapping(value = "/update")
    public BaseResponseVo update(Group  group,HttpServletRequest request,HttpServletResponse response) {
        System.out.println("123");
        try {

            if(group.getId()==0 || group.getId()==null){
                return new BaseResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            Group param =new Group();
            param.setName(group.getName());
            List<Group> list = groupService.selectByStatement(param);
            if(list!=null){
                return new DataResponseVo<Object>(StatusCode.FILE_EXIT.getCode(),StatusCode.FILE_EXIT.getMessage());
            }
            if(StrUtils.isNotEmpty(group.getServiceField())){
                group.setServiceField(group.getServiceField().replace(",",";"));
            }
            int a = groupService.update(group);
            if(a<1){
                return new BaseResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
            }
            return  new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }

    }

    @ResponseBody
    @RequestMapping(value = "/queryProvince")
    public DataResponseVo<Object> queryProvince(){
        try{
            List<AreaProvince> list = areaService.queryProvince();
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(),StatusCode.SUCCESS.getMessage(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }

    }

    @ResponseBody
    @RequestMapping(value = "/queryCity")
    public DataResponseVo<Object> queryCity(@RequestParam(value = "code")String code){
        try {
            List<AreaCity> list = areaService.queryCity(code);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryDistrict")
    public DataResponseVo<Object> queryDistrict(@RequestParam(value = "code")String code){
        try {
            List<AreaDistrict> list = areaService.queryDistrict(code);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryService")
    public DataResponseVo<Object> queryService(){
        try {
            List<GroupServiceDomain> list = groupServiceDomainService.selectAll();
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

}











